package cn.wolfcode.web.controller;

import cn.wolfcode.result.CustomerReportQueryObject;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.poi.util.SystemOutLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: 报表控制器类
 * @Author: 伍家钰
 * @Version: 1.0
 */
@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo) {
        PageInfo pageInfo = customerReportService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "customerReport/list";
    }

    /**
     * 跳转到柱状图
     *
     * @return :跳转到柱状图页面
     */
    @RequestMapping("/listBar")
    public String listBir(Model model, CustomerReportQueryObject qo) {
        // //获取所有数据(不分页)
        List<HashMap> customerReports = customerReportService.selectAll(qo);
        // 创建两个数组分别存放x,y轴的数据
        List x = new ArrayList();
        List y = new ArrayList();
        // 遍历分组数据
        for (HashMap customerReport : customerReports) {
            // 从map中获取数据存入集合中
            x.add(customerReport.get("groupType"));
            y.add(customerReport.get("number"));
        }
        // 将数据共享到页面(freeMark不能直接显示非字符串的数据:时间,集合和数组,所以先转成json格式的数据,因为json格式的内容都是带引号)
        model.addAttribute("x", JSON.toJSONString(x));
        model.addAttribute("y", JSON.toJSONString(y));
        // 动态显示分组类型
        model.addAttribute("groupType", MessageUtil.changMsg(qo));
        return "customerReport/listBar";
    }

    /**
     * 跳转到饼图
     *
     * @return :跳转到饼图页面
     */
    @RequestMapping("/listPie")
    public String listPie(Model model, CustomerReportQueryObject qo) {
        // //获取所有数据(不分页)
        List<HashMap> customerReports = customerReportService.selectAll(qo);
        // 创建两个数组分别存放x,y轴的数据
        List legend = new ArrayList();
        List series = new ArrayList();
        // 遍历分组数据
        for (HashMap customerReport : customerReports) {
            // 从map中获取数据存入集合中
            legend.add(customerReport.get("groupType"));
            // 创建一个map集合
            HashMap temp = new HashMap();
            temp.put("name",customerReport.get("groupType"));
            temp.put("value",customerReport.get("number"));
            // 将map存入集合中
            series.add(temp);
        }
        // 共享到作用域
        model.addAttribute("legend",JSON.toJSONString(legend));
        model.addAttribute("series",JSON.toJSONString(series));
        // 动态显示分组类型
        model.addAttribute("groupType", MessageUtil.changMsg(qo));
        return "customerReport/listPie";
    }
}
